package com.lhj.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lhj.reggie.common.R;
import com.lhj.reggie.entity.Employee;
import com.lhj.reggie.mapper.EmployeeMapper;
import com.lhj.reggie.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;

    @Override
    public R<Employee> login(HttpServletRequest request, Employee employee) {
        //1.将密码进行md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2.根据用户名查询数据库
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<Employee>();
        lqw.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeMapper.selectOne(lqw);
        //3.如果没有查询到结果就返回失败
        if (emp == null) {
            return R.error("登录失败");
        }
        //4.继续对比密码是否正确
        if (!emp.getPassword().equals(password)) {
            return R.error("密码错误");
        }
        //5.继续判断是否拥有权限
        if (emp.getStatus() == 0) {
            return R.error("该账户没有权限");
        }
        //6.登录成功，将员工id存入session并返回登录成功结果
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);

    }

    @Override
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("登出成功");
    }


    public R<String> insert(HttpServletRequest request, @RequestBody Employee employee) {
        //设置初始密码为123456并将密码进行MD5加密存入数据库
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());

        //获取当前登录用户id
//        Long empid = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(empid);
//        employee.setUpdateUser(empid);
        employeeMapper.insert(employee);

        return R.success("新增员工成功");
    }

    public R<Page> page(int page, int pageSize, String name) {
        //构造分页构造器
        Page page1 = new Page(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        //添加过滤条件
        lqw.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        //添加排序条件
        lqw.orderByDesc(Employee::getUpdateTime);

        employeeMapper.selectPage(page1, lqw);

        return R.success(page1);
    }

    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
//        Long empid = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(empid);
        employeeMapper.updateById(employee);
        return R.success("员工信息修改成功");
    }

    public R<Employee> getById(@PathVariable Long id){
        Employee employee = employeeMapper.selectById(id);
        if(employee!=null){
            return R.success(employee);
        }
        return R.error("没有查询到相关员工信息");
    }
}
